/**
 * 拖拽插件 - 实现班次的拖拽移动功能
 * 允许用户通过拖拽来调整班次安排
 */

import type { Plugin, Shift } from '../../types'

export const DragDropPlugin: Plugin = {
  id: 'drag-drop',
  name: 'Drag & Drop Plugin',
  version: '1.0.0',
  description: '提供班次拖拽功能',

  onInstall(context) {
    let draggedShift: Shift | null = null

    // 监听拖拽开始
    const unsubDragStart = context.on('shift:dragStart', (shift: Shift) => {
      draggedShift = shift
      console.log('[DragDropPlugin] Drag started:', shift)
    })

    // 监听单元格点击，检查是否有拖拽的班次需要放置
    const unsubCellClick = context.on(
      'cell:click',
      (data: { employeeId: string | number; date: string }) => {
        if (draggedShift) {
          // 更新班次位置
          context.updateShift(draggedShift.id, {
            employeeId: data.employeeId,
            date: data.date,
          })

          context.emit('shift:dragEnd', {
            shift: draggedShift,
            newEmployeeId: data.employeeId,
            newDate: data.date,
          })

          draggedShift = null
        }
      }
    )

    // 在卸载时清理监听
    this.onUninstall = () => {
      unsubDragStart()
      unsubCellClick()
      console.log('[DragDropPlugin] Uninstalled')
    }
  },

  onEnable(_context) {
    console.log('[DragDropPlugin] Enabled')
  },

  onDisable(_context) {
    console.log('[DragDropPlugin] Disabled')
  },
}
